﻿using System.Configuration;
using System.IO;
using System.Text;
using System.Web.UI;
using Channelvn.Cached.Common;
using System.Web;
using System;
using System.Reflection;

namespace Channelvn.Cached.Base
{
    public class PageBase : Page
    {
        public string PageCacheName
        {
            get
            {
                return string.Format(Constants.CACHE_NAME_FORMAT_HTML_PAGE_CONTENT, Lib.QueryString.CategoryID, Lib.QueryString.NewsID, Lib.QueryString.ThreadID, Lib.QueryString.PageIndex, Lib.QueryString.ViewByDate);
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            string m_CacheContent;
            StringBuilder strBuilder = new StringBuilder();
            using (StringWriter strWriter = new StringWriter(strBuilder))
            {
                using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))
                {
                    base.Render(htmlWriter);
                    m_CacheContent = strBuilder.ToString().Trim();
                    if (ConfigurationManager.AppSettings["AllowWrite1Line"] == "1")
                    {
                        m_CacheContent = m_CacheContent.Replace("\t", string.Empty);
                        m_CacheContent = m_CacheContent.Replace("    ", string.Empty);
                        m_CacheContent = m_CacheContent.Replace("  ", " ");
                    }
                }
            }
            if (CacheController.IsAllowDistributedCached)
            {
                CacheSettings settings = CacheSettings.GetCurrentSettings();
                CacheSettings.PageSetting pageSetting = settings.GetPageSetting(Request.FilePath);

                if (pageSetting.EnableCache && pageSetting.UpdateCacheByWeb)
                {
                    if (!string.IsNullOrEmpty(pageSetting.Category))
                    {
                        CacheController.Add(pageSetting.Category, PageCacheName, m_CacheContent, pageSetting.CacheExpire);
                    }
                    else
                    {
                        CacheController.Add(PageCacheName, m_CacheContent, pageSetting.CacheExpire);
                    }
                }
                else
                {
                    CacheController.Add(Lib.QueryString.ParentCategory.ToString(), PageCacheName, m_CacheContent, CacheController.DefauleExprieCategory);
                }
            }

            //Write html
            writer.Write(m_CacheContent);
        }
        private class RewriteFormHtmlTextWriter : HtmlTextWriter
        {
            private string _formAction;

            public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
                : base(writer)
            {

            }
            public RewriteFormHtmlTextWriter(System.IO.TextWriter writer, string action)
                : base(writer)
            {
                if (!string.IsNullOrEmpty(action))
                {
                    this._formAction = action;
                }
            }

            public override void RenderBeginTag(string tagName)
            {
                if (tagName.IndexOf("form") >= 0)
                {
                    base.RenderBeginTag(tagName);
                }
            }

            public override void WriteAttribute(string name, string value, bool fEncode)
            {
                if ("action" == name && !string.IsNullOrEmpty(this._formAction))
                {
                    value = this._formAction;
                }
                base.WriteAttribute(name, value, fEncode);
            }
        }
    }
}
